package com.spring.trans.service;

import com.spring.trans.dao.AccountInfoDao;
import com.spring.trans.dao.ProductInfoDao;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 每一行代码，都是改变世界的力量
 *
 * @author 郭帅
 * @date 2021/12/25 5:41 下午
 */
@Component
public class PayServiceImpl implements PayService {

    @Resource
    private AccountInfoDao accountInfoDao;

    @Resource
    private ProductInfoDao productInfoDao;


    @Transactional(rollbackFor = Exception.class)
    public void pay(String accountId, double money) {
        double retVal = accountInfoDao.updateAccountBalance(accountId, money);
        ((PayService) AopContext.currentProxy()).updateProductStore(1);
        System.out.println(1 / 0);
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void updateProductStore(Integer productId) {
        System.out.println(productId);
    }
}
